package com.sddm.printServer.service.impl;


import com.sddm.printServer.entiy.CallBack;
import com.sddm.printServer.entiy.Print;
import com.sddm.printServer.enums.CallBackEnum;
import com.sddm.printServer.service.DefaultResponseAdapter;
import com.sddm.printServer.service.PrintPdfHandler;
import com.sddm.printServer.service.SocketMessageAdapter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.websocket.Session;

import java.awt.print.PrinterJob;

import java.util.List;

/**
 * @author wyh
 * @version 1.0
 * @date 2022/4/7 13:47
 */
@Service
public class SocketMessageAdapterImpl implements SocketMessageAdapter {

    @Autowired
    private PrintPdfHandler printPdfHandler;


    @Autowired
    private DefaultResponseAdapter defaultResponseAdapter;

    @Override
    public void onMessageProcess(List<Print> print, Session session) {
        if (print.size() == 1) {
            if ("打印提交".equals(print.get(0).getOper())) {
                printPdfHandler.printPdf(print.get(0), session);
            }
        } else if (print.size() > 1) {
            printPdfHandler.MultiplePrintPdf(print, session);
        }

    }

    @Override
    public CallBack onOpenProcess(String customerId, Session session) {
        try {
            return new CallBack(200, customerId, "获得打印机列表", PrinterJob.lookupPrintServices());
        } catch (Exception e) {
            defaultResponseAdapter.sendMessage(new CallBack(CallBackEnum.GET_PRINTLIST_ERROR.getCode()
                    ,customerId,CallBackEnum.GET_PRINTLIST_ERROR.getMessage()),session);
        }
        return null;
    }
}
